package cn.eng.org.controller;

import cn.eng.basic.utils.AjaxResult;
import cn.eng.basic.utils.PageList;
import cn.eng.org.domain.Employee;
import cn.eng.org.query.EmployeeQuery;
import cn.eng.org.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private IEmployeeService employeeService;
    /**
     * 查询单个对象
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Employee loadById(@PathVariable("id") Long id) {
        return employeeService.loadById(id);
    }

    /**
     * 查询所有对象
     *
     * @return
     */
    @GetMapping()
    public List<Employee> loadAll() {
        return employeeService.loadAll();
    }

    /**
     * 添加和修改
     *
     * @param employee
     * @return
     */
    @PutMapping()
    public AjaxResult saveOrUpdate(@RequestBody Employee employee) {
        try {
            if (employee.getId() != null) {
                employeeService.update(employee);
            } else {
                employeeService.save(employee);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("系统繁忙， 请稍后再试");
        }
    }

    /**
     * 删除单个对象
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            employeeService.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("系统繁忙，请稍后再试");
        }
    }

    /**
     * 分页查询
     * @param employeeQuery
     * @return
     */
    @PostMapping()
    public PageList<Employee> queryPage(@RequestBody EmployeeQuery employeeQuery) {
        return employeeService.queryPage(employeeQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping()
    public AjaxResult patchDel(@RequestBody Long[] ids) {
        try {
            employeeService.patchDel(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult().setSuccess(false).setMsg("操作失败");
        }
    }
    @GetMapping("/list/{shopId}")
    public List<Employee> list(@PathVariable("shopId") Long shopId){
        return employeeService.list(shopId);
    }
}
